package com.facebook.listener;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

import com.imoblife.brainwavestus.share.BaseRequestListener;
import com.imoblife.brainwavestus.share.Utility;

/*
 * Callback for fetching current user's name, picture, uid.
 */
public class UserRequestListener extends BaseRequestListener {
	
	private Activity mActivity;
	private Handler mHandler;
	private TextView mText;
	private ImageView mUserPic;
	
	public UserRequestListener(){}
	public UserRequestListener(Activity activity){
		mActivity = activity;
		mHandler = new Handler();
	}
	

	public void onComplete(final String response, final Object state) {
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(response);

			final String picURL = jsonObject.getString("picture");
			final String name = jsonObject.getString("name");
			final String email = (String)jsonObject.get("email");
			Utility.userUID = jsonObject.getString("id");

			mHandler.post(new Runnable() {
				public void run() {
					mText.setText("Welcome " + name + "!" + " UID :"
							+ Utility.userUID +" email : "+email);
					mUserPic.setImageBitmap(Utility.getBitmap(picURL));
				}
			});

		} catch (JSONException e) {
			
			e.printStackTrace();
		}
	}

}
